using System;
using System.Windows.Input;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;

namespace AvaloniaUI.Modal.Controls
{
    /// <summary>
    /// 模态对话框控件，负责显示对话框的内容、标题和按钮
    /// </summary>
    public class ModalDialog : TemplatedControl
    {
        #region 依赖属性

        /// <summary>
        /// 定义Content依赖属性
        /// </summary>
        public static readonly StyledProperty<object> ContentProperty =
            AvaloniaProperty.Register<ModalDialog, object>(nameof(Content));

        /// <summary>
        /// 定义Header依赖属性
        /// </summary>
        public static readonly StyledProperty<object> HeaderProperty =
            AvaloniaProperty.Register<ModalDialog, object>(nameof(Header));

        /// <summary>
        /// 定义Footer依赖属性
        /// </summary>
        public static readonly StyledProperty<object> FooterProperty =
            AvaloniaProperty.Register<ModalDialog, object>(nameof(Footer));

        /// <summary>
        /// 定义Result依赖属性
        /// </summary>
        public static readonly StyledProperty<object> ResultProperty =
            AvaloniaProperty.Register<ModalDialog, object>(nameof(Result));

        /// <summary>
        /// 定义CloseCommand依赖属性
        /// </summary>
        public static readonly StyledProperty<ICommand> CloseCommandProperty =
            AvaloniaProperty.Register<ModalDialog, ICommand>(nameof(CloseCommand));

        #endregion

        #region 属性

        /// <summary>
        /// 获取或设置对话框内容
        /// </summary>
        public object Content
        {
            get => GetValue(ContentProperty);
            set => SetValue(ContentProperty, value);
        }

        /// <summary>
        /// 获取或设置对话框标题
        /// </summary>
        public object Header
        {
            get => GetValue(HeaderProperty);
            set => SetValue(HeaderProperty, value);
        }

        /// <summary>
        /// 获取或设置对话框底部内容（通常是按钮）
        /// </summary>
        public object Footer
        {
            get => GetValue(FooterProperty);
            set => SetValue(FooterProperty, value);
        }

        /// <summary>
        /// 获取或设置对话框结果
        /// </summary>
        public object Result
        {
            get => GetValue(ResultProperty);
            set => SetValue(ResultProperty, value);
        }

        /// <summary>
        /// 获取或设置关闭命令
        /// </summary>
        public ICommand CloseCommand
        {
            get => GetValue(CloseCommandProperty);
            set => SetValue(CloseCommandProperty, value);
        }

        #endregion

        /// <summary>
        /// 初始化ModalDialog类的新实例
        /// </summary>
        public ModalDialog()
        {
            // 阻止点击事件冒泡，防止点击对话框内容时触发遮罩层的点击事件
            this.PointerPressed += (s, e) => e.Handled = true;
        }

        /// <summary>
        /// 当模板应用到控件时调用
        /// </summary>
        protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
        {
            base.OnApplyTemplate(e);

            var closeButton = e.NameScope.Find<Button>("PART_CloseButton");
            if (closeButton != null)
            {
                closeButton.Click += (s, e) => 
                {
                    if (CloseCommand != null && CloseCommand.CanExecute(null))
                    {
                        CloseCommand.Execute(null);
                    }
                };
            }
        }
    }
}